<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "people".
 *
 * @property integer $id
 * @property string $nick_name
 * @property string $real_name
 * @property string $mobile
 * @property integer $sex
 * @property string $address
 * @property string $mark
 * @property string $create_at
 */
class People extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'people';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['mobile'], 'required'],
            [['sex'], 'integer'],
            [['create_at'], 'safe'],
            [['nick_name', 'real_name', 'mobile'], 'string', 'max' => 50],
            [['address'], 'string', 'max' => 255],
            [['mark'], 'string', 'max' => 500],
            [['mobile'], 'unique','message'=>'手机号已存在'],
            [['mobile'], 'match', 'pattern' => '/^(0|86|17951)?(13[0-9]|15[012356789]|17[013678]|18[0-9]|14[57])[0-9]{8}$/','message'=>'手机号不正确，只允许国内号码']
        ];
    }
    
    public function beforeSave($insert)
    {
        if($this->isNewRecord){
            $this->create_at = date("Y-m-d H:i:s");
        }
        return parent::beforeSave($insert); // TODO: Change the autogenerated stub
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'nick_name' => Yii::t('app', '昵称'),
            'real_name' => Yii::t('app', '真实姓名'),
            'mobile' => Yii::t('app', '手机号码'),
            'sex' => Yii::t('app', '性别'),
            'address' => Yii::t('app', '住址'),
            'mark' => Yii::t('app', '备注'),
            'create_at' => Yii::t('app', '创建时间'),
        ];
    }

    public function getGames()
    {
        // 客户和订单通过 Game.uid -> id 关联建立一对多关系
        return $this->hasMany(Game::className(), ['uid' => 'id'])->orderBy("create_at desc");
    }
}
